package com.zyl.Ecs.common.domain.auth.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyl.Ecs.common.domain.auth.ext.AccountExt;
import com.zyl.Ecs.common.utils.ObjectUtil;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.auth.domain.vo
 * @ClassName: AccountVO
 * @Description TODO (用户VO)
 * @Author 翟玉龙
 * @Date 2024/12/4-16:57
 */
@Data
@Builder
public class AccountVO {

    @JsonProperty(value = "id")
    private Integer id;

    @JsonProperty(value = "username")
    private String username;

    @JsonProperty(value = "password", access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    @JsonProperty(value = "telephone")
    private String telephone;

    @JsonProperty(value = "status")
    private Integer status;

    @JsonProperty(value = "registerTime")
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime registerTime;

    @JsonProperty(value = "roleIds")
    private List<Integer> roleIds;

    @JsonProperty(value = "roles")
    private List<RoleVO> roles;

    public static AccountVO toVO(AccountExt accountExt) {
        if (ObjectUtil.isNull(accountExt)) {
            return null;
        }
        return AccountVO.builder()
                .id(accountExt.getId())
                .username(accountExt.getUsername())
                .password(accountExt.getPassword())
                .telephone(accountExt.getTelephone())
                .status(accountExt.getStatus())
                .registerTime(accountExt.getRegisterTime())
                .roleIds(accountExt.getRoleIds())
                .roles(Optional.ofNullable(accountExt.getRoles())
                        .map(list -> list.stream()
                                .map(RoleVO::toVO)
                                .collect(Collectors.toList()))
                        .orElse(null))
                .build();
    }
}
